(function() {
    'use strict';

    angular
        .module('milkbeeApp')
        .controller('CardHomeController',CardHomeController);

    CardHomeController.$inject = ['$scope','$uibModalInstance', 'entity', 'Card'];

    function CardHomeController($scope,$uibModalInstance, entity, Card) {
        var vm = this;

        vm.card = entity;
        vm.clear = clear;
        vm.save=save;
        vm.confirmDelete = confirmDelete;

        function clear () {
            $uibModalInstance.dismiss('cancel');
        }

        function confirmDelete (id) {
            Card.delete({id: id},
                function () {
                    $uibModalInstance.close(true);
                });
        }
        
        function save () {
            vm.isSaving = true;
            if (vm.card.id !== null) {
            	vm.card.recommend=true;
            	Card.update(vm.card, onSaveSuccess, onSaveError);
            } else {
            	Card.save(vm.card, onSaveSuccess, onSaveError);
            }
        }
        
        function onSaveSuccess (result) {
            $scope.$emit('milkbeeApp:cardUpdate', result);
            $uibModalInstance.close(result);
            vm.isSaving = false;
        }

        function onSaveError () {
            vm.isSaving = false;
        }
    }
})();
